#include "media_snap_manager.h"

#include "utils_log.h"
#include <memory>

namespace El {
namespace Media {

ISnap &ISnap::GetInstance(uint32_t ch, int32_t type)
{
    static std::map<std::pair<uint32_t, int32_t>, SnapManager *> instances;
    static std::mutex mutex;

    std::lock_guard<std::mutex> lock(mutex);
    auto key = std::make_pair(ch, type);
    auto it = instances.find(key);
    if (it == instances.end()) {
        SnapManager *instance = new SnapManager(ch, type);
        instances.emplace(key, instance);
        return *instance;
    }
    return *it->second;
}

SnapManager::SnapManager(uint32_t ch, int32_t type) : ch_(ch), type_(type) {}

bool SnapManager::Start()
{
    if (isStarted_) {
        EL_INFO("Snap module already started, channel: {}, type: {}", ch_, type_);
        return true;
    }

    EL_INFO("Start snap module, channel: {}, type: {}", ch_, type_);
    isStarted_ = true;
    return true;
}

void SnapManager::Stop()
{
    if (!isStarted_) {
        EL_INFO("Snap module already stopped, channel: {}, type: {}", ch_, type_);
        return;
    }

    EL_INFO("Stop snap module");
    isStarted_ = false;
}

int32_t SnapManager::Register(const Proc &proc)
{
    return callbackManager_.Register(proc);
}

void SnapManager::Unregister(int32_t handle)
{
    callbackManager_.Unregister(handle);
}

bool SnapManager::ManualSnap()
{
    EL_DEBUG("Manual snap, channel: {}, type: {}", ch_, type_);
    return true;
}

} // namespace Media
} // namespace El
